<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="title" label="标题" required>
				<uni-easyinput placeholder="标题" v-model="formData.title" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="author" label="作者">
				<uni-easyinput placeholder="请输入作者名称" v-model="formData.author" trim="right"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="avatar" label="封面">
				<uni-file-picker return-type="object" v-model="formData.avatar"></uni-file-picker>
			</uni-forms-item>
			<uni-forms-item name="content" label="文章内容" required>
				<div id="div1"></div>
				<!-- <uni-easyinput placeholder="文章内容" v-model="" trim="right"></uni-easyinput> -->
			</uni-forms-item>
			<uni-forms-item name="view_count" label="阅读数量">
				<uni-easyinput placeholder="阅读数量" type="number" v-model="formData.view_count"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="is_essence" label="推荐">
				<switch @change="binddata('is_essence', $event.detail.value)" :checked="formData.is_essence"></switch>
			</uni-forms-item>
			<uni-forms-item name="publish_date" label="发表时间">
				<uni-datetime-picker return-type="timestamp" v-model="formData.publish_date"></uni-datetime-picker>
			</uni-forms-item>
			<uni-forms-item name="article_status" label="文章状态">
				<uni-data-checkbox v-model="formData.article_status" :localdata="formOptions.article_status_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px">
					<button class="uni-button" style="width: 100px">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
import { validator } from '../../js_sdk/validator/xzs-news.js';
import E from 'wangeditor';
const db = uniCloud.database();
const dbCmd = db.command;
const dbCollectionName = 'xzs-news';
let editor = null;
function getValidator(fields) {
	let result = {};
	for (let key in validator) {
		if (fields.includes(key)) {
			result[key] = validator[key];
		}
	}
	return result;
}

export default {
	data() {
		let formData = {
			title: '',
			author: '',
			avatar: null,
			content: '',
			view_count: 50,
			is_essence: null,
			publish_date: null,
			article_status: 1
		};
		return {
			formData,
			formOptions: {
				article_status_localdata: [
					{
						value: 0,
						text: '草稿箱'
					},
					{
						value: 1,
						text: '已发布'
					}
				]
			},
			rules: {
				...getValidator(Object.keys(formData))
			},
			editor: null,
			toolbarConfig: {},
			editorConfig: { placeholder: '请输入内容...' },
			mode: 'default' // or 'simple'
		};
	},
	onReady() {
		this.$refs.form.setRules(this.rules);
		this.onWangEdit();
	},
	beforeDestroy() {
		const editor = this.editor;
		if (editor == null) return;
		editor.destroy(); // 组件销毁时，及时销毁编辑器
	},
	methods: {
		//初始化wangedit
		onWangEdit() {
			editor = new E('#div1');
			editor.config.zIndex = 0;
			editor.config.onblur = (newHtml) => {
				this.formData.content = newHtml;
			};

			editor.config.customUploadImg = function (resultFiles, insertImgFn) {
				resultFiles.forEach((item) => {
					let path = URL.createObjectURL(item);
					let name = item.name;
					uniCloud
						.uploadFile({
							filePath: path,
							cloudPath: name
						})
						.then((res) => {
							insertImgFn(res.fileID);
						});
				});
			};
			editor.config.customUploadImg = function (resultFiles, insertImgFn) {
				console.log(resultFiles, 'file');
				resultFiles.forEach((item) => {
					let path = URL.createObjectURL(item);
					let name = item.name;
					uniCloud
						.uploadFile({
							filePath: path,
							cloudPath: name
						})
						.then((res) => {
							let imgUrl = res.fileID;
							insertImgFn(imgUrl);
						});
				});
				return;
				insertImgFn(imgUrl);
			};
			editor.create();
		},
		onCreated(editor) {
			this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
		},
		/**
		 * 验证表单并提交
		 */
		submit() {
			uni.showLoading({
				mask: true
			});
			this.$refs.form
				.validate()
				.then((res) => {
					return this.submitForm(res);
				})
				.catch(() => {})
				.finally(() => {
					uni.hideLoading();
				});
		},

		/**
		 * 提交表单
		 */
		submitForm(value) {
			value.content = editor.txt.html();
			// 使用 clientDB 提交数据
			return db
				.collection(dbCollectionName)
				.add(value)
				.then((res) => {
					uni.showToast({
						title: '新增成功'
					});
					this.getOpenerEventChannel().emit('refreshData');
					setTimeout(() => uni.navigateBack(), 500);
				})
				.catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					});
				});
		}
	}
};
</script>
<style lang="scss">
.w-e-bar-item {
	button {
		padding: 0 8px !important;
		white-space: nowrap !important;
		height: 32px !important;
		display: inline-flex !important;
		overflow: hidden !important;
		border: none !important;
		align-items: center !important;
		cursor: pointer !important;
		justify-content: center !important;
		line-height: 1 !important;
	}
}
</style>
